import Vue from 'vue'
import VueRouter from 'vue-router'

import Vuex from 'vuex'
Vue.use(Vuex)

//每次刚进入网站，肯定会调用main.js，我们将本地的存储的数据读取出来，放到store中
var car = JSON.parse(localStorage.getItem('car')||'[]')

var store = new Vuex.Store({
    state:{ //this.$store.state.xxxx
     //   car:car
        car:car //将购物车中的商品数据，用一个数组存储起来，在这个数组总，存储一些商品的对象，{id:商品的id,count:要购买的数量,price:商品的单价,select:是否选中}
    },
    mutations:{ //this.$store.commit('方法的名称','按需导入唯一参数')
        addToCar(state,goodsinfo){
            var flag = false
            state.car.some(item =>{
                if(item.id== goodsinfo.id){
                    item.count += parseInt(goodsinfo.count)
                    flag = true
                    return true
                }
                
            })
            if(!flag){
                state.car.push(goodsinfo)
            }

            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateGoodsInfo(state,goodsinfo){
            state.car.some(item =>{
                if(item.id ==goodsinfo.id){
                    item.count = parseInt(goodsinfo.count)
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        removeFromCar(state,id){
            state.car.forEach((item,i) =>{
                if(item.id ==id){
                    state.car.splice(i,1)
                }
                return true;
            })

            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateGoodsSelected(state,goodsinfo){
            state.car.forEach(item =>{
                if(item.id ==goodsinfo.id){
                    item.selected = goodsinfo.selected
                }
                return true
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },
    getters:{ //this.$store.getters.xxxx
        getAllCount(state){
            var c =0;
            state.car.forEach(item =>{
                    c+=item.count
            })
            return c
        },
        getGoodCount(state){
            var o= {};
            state.car.forEach(item =>{
                o[item.id] = item.count
            })
            return o
        },
        getGoodsSelected(state){
            var o={}
            state.car.forEach(item =>{
                o[item.id] = item.selected
            })
            return o
        },
        getGoodsCountAndAmount(state){
            var o={
                count:0,
                amount:0
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.count += item.count
                    o.amount += item.count *item.price
                }
                
            })
            return o
        }
    }
})



import router from './router.js'
Vue.use(VueRouter)

import VueResource from 'vue-resource'

Vue.use(VueResource)

Vue.http.options.root = 'http://www.liulongbin.top:3005';
Vue.http.options.emulateJSON = true;

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

import app from './App.vue'

// import {Header,Swipe, SwipeItem,Button} from 'mint-ui'


// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.component(Header.name, Header);

import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//安装图片缩略插件
import VuePreview from 'vue-preview'
 
Vue.use(VuePreview)

import moment from 'moment'



Vue.filter('dateFormat',function(dateStr,pattern="YYYY-MM-DD HH:mm:ss"){
    return  moment(dateStr).format(pattern)
})

var vm = new Vue({
    el:'#app',
    render: c => c(app),
    router,
    store
})